package vn.orderonline.core.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.Food;

@Repository
public class FoodDaoImpl extends GenericDaoImpl<Food, Integer> implements
		FoodDao {

	@Override
	public Boolean checkExist(Integer menuFoodId, String foodName) {
		String hql = "From MenuFood mf, MenuFoodFood mff, Food f "
				+ "Where mf.menuFoodId = mff.id.menuFoodId And mff.id.foodId = f.foodId And "
				+ "mf.menuFoodId = :menuFoodId And f.foodName = :foodName";
		Query query = getSession().createQuery(hql);
		query.setParameter("menuFoodId", menuFoodId);
		query.setParameter("foodName", foodName);
		return (query.uniqueResult() != null) ? true : false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getListFoodByRestaurantIdAndMenuFoodId(Integer resId,
			Integer menuFoodId) {
		String hql = "Select res.restaurantId, res.restaurantName, mf.menuFoodId, mf.menuFoodName, f "
				+ "From Restaurant res, RestaurantMenuFood rmf, MenuFood mf, MenuFoodFood mff, Food f "
				+ "Where res.restaurantId = rmf.id.restaurantId And rmf.id.menuFoodId = mf.menuFoodId And "
				+ "mf.menuFoodId = mff.id.menuFoodId And mff.id.foodId = f.foodId And res.restaurantId = :restaurantId "
				+ "And mf.menuFoodId = :menuFoodId";
		Query query = getSession().createQuery(hql);
		query.setParameter("restaurantId", resId);
		query.setParameter("menuFoodId", menuFoodId);
		return query.list();
	}

	@Override
	public Object[] getFood(Integer foodId) {
		String hql = "Select res.restaurantId, res.restaurantName, mf.menuFoodId, mf.menuFoodName, f "
				+ "From Restaurant res, RestaurantMenuFood rmf, MenuFood mf, MenuFoodFood mff, Food f "
				+ "Where res.restaurantId = rmf.id.restaurantId And rmf.id.menuFoodId = mf.menuFoodId And "
				+ "mf.menuFoodId = mff.id.menuFoodId And mff.id.foodId = f.foodId And f.foodId = :foodId";
		Query query = getSession().createQuery(hql);
		query.setParameter("foodId", foodId);
		return (Object[]) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> searchFood(Integer resId, String foodName,
			String foodTitle, Float oldPrice, Float newPrice) {
		String hql = "Select res.restaurantId, res.restaurantName, mf.menuFoodId, mf.menuFoodName, f "
				+ "From Restaurant res, RestaurantMenuFood rmf, MenuFood mf, MenuFoodFood mff, Food f "
				+ "Where res.restaurantId = rmf.id.restaurantId And rmf.id.menuFoodId = mf.menuFoodId And "
				+ "mf.menuFoodId = mff.id.menuFoodId And mff.id.foodId = f.foodId ";
		StringBuilder str = new StringBuilder(hql);
		if (resId != null) {
			str.append("And res.restaurantId = :restaurantId ");
		}
		if (!StringUtils.isNullOrEmpty(foodName)) {
			str.append("And f.foodName like :foodName ");
		}
		if (!StringUtils.isNullOrEmpty(foodTitle)) {
			str.append("And f.foodTitle like :foodTitle ");
		}
		if (oldPrice != null) {
			str.append("And f.oldPrice = :oldPrice ");
		}
		if (newPrice != null) {
			str.append("And f.newPrice = :newPrice");
		}
		Query query = getSession().createQuery(str.toString());
		if (resId != null) {
			query.setParameter("restaurantId", resId);
		}
		if (!StringUtils.isNullOrEmpty(foodName)) {
			query.setParameter("foodName", "%" + foodName + "%");
		}
		if (!StringUtils.isNullOrEmpty(foodTitle)) {
			query.setParameter("foodTitle", "%" + foodTitle + "%");
		}
		if (oldPrice != null) {
			query.setParameter("oldPrice", oldPrice);
		}
		if (newPrice != null) {
			query.setParameter("newPrice", newPrice);
		}
		return query.list();
	}

}
